package cn.hyatt.auth.service.impl;

import cn.hyatt.auth.dto.systemMenu.SystemMenuFindDto;
import cn.hyatt.auth.dto.systemMenu.SystemMenuSaveDto;
import cn.hyatt.auth.dto.systemMenu.SystemMenuUpdateDto;
import cn.hyatt.auth.dto.systemMenu.SystemMenuVo;
import cn.hyatt.auth.entity.SystemMenu;
import cn.hyatt.auth.enums.MenuType;
import cn.hyatt.auth.event.SystemMenuEvent;
import cn.hyatt.auth.mapper.SystemMenuMapper;
import cn.hyatt.auth.service.SystemMenuService;
import cn.hyatt.common.exception.BusinessException;
import cn.hyatt.common.utils.ValidationUtil;
import cn.hyatt.core.utils.PageUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

/**
 * 系统菜单-服务实现
 *
 * @author hyatt
 */
@Slf4j
@Service
@Primary
@AllArgsConstructor
public class SystemMenuServiceImpl extends ServiceImpl<SystemMenuMapper, SystemMenu> implements SystemMenuService {

    private ApplicationEventPublisher publisher;

    @Override
    public List<SystemMenu> findByList(SystemMenuFindDto dto) {
        return baseMapper.findByList(dto);
    }

    @Override
    public IPage<SystemMenu> findByPage(SystemMenuFindDto dto) {
        return baseMapper.findByPage(PageUtil.page(dto),dto);
    }

    @Override
    public List<SystemMenu> findByTypeAndRole(List<MenuType> types, List<Long> roleIds) {
        return baseMapper.findByTypeAndRole(types,roleIds);
    }

    @Override
    @Transactional
    public void save(SystemMenuSaveDto dto) {
        // Copy 属性
        SystemMenu entity = new SystemMenu();
        BeanUtils.copyProperties(dto, entity);
        // 验证数据内容
        ValidationUtil.validate(entity);
        // 插入数据
        baseMapper.insert(entity);

        // 发布事件
        publisher.publishEvent(new SystemMenuEvent.Save(entity));
    }

    @Override
    @Transactional
    public void update(Long id, SystemMenuUpdateDto dto) {
        // 获取修改对象
        Optional<SystemMenu> optional = getOptById(id);
        SystemMenu entity = optional.orElseThrow(
                () -> new BusinessException("找不到Id为(" + id + ")的数据")
        );
        // 保留旧实体记录
        SystemMenu oldEntity = new SystemMenu();
        BeanUtils.copyProperties(entity, oldEntity);
        // Copy 属性
        BeanUtils.copyProperties(dto, entity);
        // 验证数据内容
        ValidationUtil.validate(entity);
        // 按Id更新
        baseMapper.updateById(entity);

        // 发布事件
        publisher.publishEvent(new SystemMenuEvent.Update(oldEntity, entity));
    }

    @Override
    @Transactional
    public void deleteById(Long id) {
        // 按Id删除
        baseMapper.deleteById(id);
        // 发布事件
        publisher.publishEvent(new SystemMenuEvent.Delete(id));
    }

    @Override
    public SystemMenuVo toVo(SystemMenu entity) {
        // Copy 属性
        SystemMenuVo vo = new SystemMenuVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }
}
